﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace JobCreateInDocs2_WinForms_Probe
{
    public static class DBSPLog
    {

        #region Members
        #endregion Members

        #region Insert
        public static Exception Insert(string parName, string parKey, string parSubKey, string parValue)
        {
            Exception ret = null;
            string connStr = KoEnvironment.DB04ConnStr;
            string sql =
@"
INSERT INTO SP_Log
(Name, [Key], SubKey, Value, MachineName)
VALUES
(?, ?, ?, ?, ?)
";
            try
            {
                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    conn.Open();
                    using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = sql;
                        cmd.Parameters.Add("parName", OleDbType.VarWChar).Value = parName;
                        cmd.Parameters.Add("parKey", OleDbType.VarWChar).Value = parKey;
                        cmd.Parameters.Add("parSubKey", OleDbType.VarWChar).Value = parSubKey;
                        cmd.Parameters.Add("parValue", OleDbType.VarWChar).Value = parValue;
                        cmd.Parameters.Add("MachineName", OleDbType.VarWChar).Value = NetInfo.MachineName;
                        int iRet = cmd.ExecuteNonQuery();
                    }
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            finally
            {
            }
            return ret;
        }
        public static Exception Insert(string parName, string parKey, string parSubKey, string parValue
            , string parMsg, int parErrId, int parWarningLevel, int parRetCode, int parCountInfo)
        {
            Exception ret = null;
            string connStr = KoEnvironment.DB04ConnStr;
            string sql =
@"
INSERT INTO SP_Log
(Name, [Key], SubKey, Value, MachineName, Msg, ErrID, WarningLevel, RetCode, CountInfo)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
";
            try
            {
                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    conn.Open();
                    using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = sql;
                        cmd.Parameters.Add("parName", OleDbType.VarWChar).Value = parName;
                        cmd.Parameters.Add("parKey", OleDbType.VarWChar).Value = parKey;
                        cmd.Parameters.Add("parSubKey", OleDbType.VarWChar).Value = parSubKey;
                        cmd.Parameters.Add("parValue", OleDbType.VarWChar).Value = parValue;
                        cmd.Parameters.Add("MachineName", OleDbType.VarWChar).Value = NetInfo.MachineName;
                        cmd.Parameters.Add("Msg", OleDbType.VarWChar).Value = parMsg;
                        cmd.Parameters.Add("ErrId", OleDbType.Integer).Value = parErrId;
                        cmd.Parameters.Add("WarningLevel", OleDbType.Integer).Value = parWarningLevel;
                        cmd.Parameters.Add("RetCode", OleDbType.Integer).Value = parRetCode;
                        cmd.Parameters.Add("CountInfo", OleDbType.Integer).Value = parCountInfo;
                        int iRet = cmd.ExecuteNonQuery();
                    }
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            finally
            {
            }
            return ret;
        }
        #endregion Insert

        #region Delete
        public static Exception Delete(DateTime dtBefore)
        {
            Exception ret = null;
            string connStr = KoEnvironment.DB04ConnStr;
            string sql =
@"
DELETE FROM SP_Log 
WHERE (DT < ?)
";
            try
            {
                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    conn.Open();
                    using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = sql;
                        cmd.Parameters.Add("dtBefore", OleDbType.VarWChar).Value = dtBefore;
                        int iRet = cmd.ExecuteNonQuery();
                    }
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            finally
            {
            }
            return ret;
        }
        #endregion Delete

    }
}
